package com.wxzz.elearing.comment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.comment.amqp.producer.CommentProducer;
import com.wxzz.elearing.comment.dto.BannedDto;
import com.wxzz.elearing.comment.dto.BannedListDto;
import com.wxzz.elearing.comment.entity.Banned;
import com.wxzz.elearing.comment.mapper.BannedMapper;
import com.wxzz.elearing.comment.service.IBannedService;
import com.wxzz.elearing.comment.service.openfeign.IEnterQueAnsApiFeign;
import com.wxzz.elearing.comment.service.openfeign.IWxUserApiFeign;
import com.wxzz.elearning.common.dto.FeignWxUserDto;
import com.wxzz.elearning.common.dto.SystemMessageMqDto;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.utils.ExcelUtils;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2021-06-15
 */
@Service
public class BannedServiceImpl extends ServiceImpl<BannedMapper, Banned> implements IBannedService {

    @Resource
    private BannedMapper bannedMapper;

    @Autowired
    private CommentProducer commentProducer;

    @Autowired
    private IEnterQueAnsApiFeign iEnterQueAnsApiFeign;

    @Override
    public ReturnVO addCommunityBanned(Banned banned){
        //插入禁言表
        banned.setModuleType(1);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",banned.getUserId());
        queryWrapper.eq("module_type",1);
        Integer integer = bannedMapper.selectCount(queryWrapper);
        if(integer>0){
            throw new ServiceException("该用户已处于封禁状态");
        }
        banned.setBannedStatus(1);
        bannedMapper.insert(banned);
        SystemMessageMqDto systemMessageMqDto = new SystemMessageMqDto();

        //如果通知类型为1并且Phone不为null 则发送短信通知
//        FeignWxUserDto feignWxUserDto= iEnterQueAnsApiFeign.selectFeignWxUserDtoByWxUserId(banned.getUserId());
//        if(banned.getInformTypes()!=null&&banned.getInformTypes()==1&& StringUtils.isNotBlank(feignWxUserDto.getPhone())){
//            systemMessageMqDto.setPhones(new ArrayList<String>() {
//                {
//                    add(feignWxUserDto.getPhone());
//                }
//            });
//        }
        //todo 小程序订阅
        systemMessageMqDto.setMessageDetail("很抱歉,您因违规已被管理员禁言,详情请在小程序内查看");
        systemMessageMqDto.setMessageTitle("社区");
        systemMessageMqDto.setWxUserIds(new ArrayList<Long>() {
            {
                add(banned.getUserId());
            }
        });
        commentProducer.ackMQSender(JSONObject.toJSONString(systemMessageMqDto));
        return new ReturnVO("禁言成功",1);
    }

    public ReturnVO addQueAnsBanned(Banned banned){
        //插入禁言表
        banned.setModuleType(0);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",banned.getUserId());
        queryWrapper.eq("module_type",0);
        Integer integer = bannedMapper.selectCount(queryWrapper);
        if(integer>0){
            throw new ServiceException("该用户已处于封禁状态");
        }
        banned.setBannedStatus(1);
        bannedMapper.insert(banned);
        SystemMessageMqDto systemMessageMqDto = new SystemMessageMqDto();

        //todo 小程序订阅
        systemMessageMqDto.setMessageDetail("很抱歉,您因违规已被管理员禁言,详情请在小程序内查看");
        systemMessageMqDto.setMessageTitle("问答");
        systemMessageMqDto.setWxUserIds(new ArrayList<Long>() {
            {
                add(banned.getUserId());
            }
        });
        commentProducer.ackMQSender(JSONObject.toJSONString(systemMessageMqDto));
        return new ReturnVO("禁言成功",1);
    }




    @Override
    public ReturnVO<JgPageVo<List<BannedListDto>>> listCommunityBanned(BannedDto bannedListDto){
        Page<Object> objects = PageHelper.startPage(bannedListDto.getCurPage(), bannedListDto.getMaxPage());
        ArrayList<BannedListDto> bannedListDtos = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("module_type",1);
        List<Banned> list = bannedMapper.selectList(queryWrapper);
        for (Banned model:list){
            BannedListDto bannedListDto1 = new BannedListDto();
            FeignWxUserDto feignWxUserDto= iEnterQueAnsApiFeign.selectFeignWxUserDtoByWxUserId(model.getUserId());
            if(feignWxUserDto.getId()==null){
                continue;
            }
            BeanUtils.copyProperties(feignWxUserDto,bannedListDto1);
            bannedListDto1.setBannedMsg(model.getBannedMsg());
            bannedListDto1.setBannedStatus(model.getBannedStatus());
            bannedListDto1.setCreateTime(model.getCreateTime());
            bannedListDto1.setUserId(model.getUserId());
            bannedListDtos.add(bannedListDto1);
        }

        long total = objects.getTotal();
        PageInfo pageInfo = new PageInfo(bannedListDtos);
        pageInfo.setTotal(total);
        return new ReturnVO("社区禁言列表查询成功", new JgPageVo<>(pageInfo));
    }

    @Override
    public ReturnVO<JgPageVo<List<BannedListDto>>> listQueAnsBanned(BannedDto bannedListDto){
        Page<Object> objects = PageHelper.startPage(bannedListDto.getCurPage(), bannedListDto.getMaxPage());
        ArrayList<BannedListDto> bannedListDtos = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("module_type",0);
        List<Banned> list = bannedMapper.selectList(queryWrapper);
        for (Banned model:list){
            BannedListDto bannedListDto1 = new BannedListDto();
            FeignWxUserDto feignWxUserDto= iEnterQueAnsApiFeign.selectFeignWxUserDtoByWxUserId(model.getUserId());
            if(feignWxUserDto.getId()==null){
                continue;
            }
            BeanUtils.copyProperties(feignWxUserDto,bannedListDto1);
            bannedListDto1.setBannedMsg(model.getBannedMsg());
            bannedListDto1.setBannedStatus(model.getBannedStatus());
            bannedListDto1.setCreateTime(model.getCreateTime());
            bannedListDto1.setUserId(model.getUserId());
            bannedListDtos.add(bannedListDto1);
        }
        long total = objects.getTotal();
        PageInfo pageInfo = new PageInfo(bannedListDtos);
        pageInfo.setTotal(total);
        return new ReturnVO("问答禁言列表查询成功", new JgPageVo<>(pageInfo));
    }


    @Override
    public void exportCommunityExcel(HttpServletResponse response){
        ArrayList<BannedListDto> bannedListDtos = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("module_type",1);
        List<Banned> list = bannedMapper.selectList(queryWrapper);
        list.forEach(model->{
            BannedListDto bannedListDto1 = new BannedListDto();
            FeignWxUserDto feignWxUserDto= iEnterQueAnsApiFeign.selectFeignWxUserDtoByWxUserId(model.getUserId());
            BeanUtils.copyProperties(feignWxUserDto,bannedListDto1);
            bannedListDto1.setBannedMsg(model.getBannedMsg());
            bannedListDto1.setBannedStatus(model.getBannedStatus());
            bannedListDto1.setCreateTime(model.getCreateTime());
            bannedListDtos.add(bannedListDto1);
        });
        try {
            ExcelUtils.exportExcel(bannedListDtos, "社区禁言表", "社区禁言", BannedListDto.class, "社区禁言", response);
        } catch (IOException e) {
            throw new ServiceException("导出异常:"+e.getMessage());
        }
    }

    @Override
    public void exportQueAnsExcel(HttpServletResponse response){
        ArrayList<BannedListDto> bannedListDtos = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("module_type",0);
        List<Banned> list = bannedMapper.selectList(queryWrapper);
        list.forEach(model->{
            BannedListDto bannedListDto1 = new BannedListDto();
            FeignWxUserDto feignWxUserDto= iEnterQueAnsApiFeign.selectFeignWxUserDtoByWxUserId(model.getUserId());
            BeanUtils.copyProperties(feignWxUserDto,bannedListDto1);
            bannedListDto1.setBannedMsg(model.getBannedMsg());
            bannedListDto1.setBannedStatus(model.getBannedStatus());
            bannedListDto1.setCreateTime(model.getCreateTime());
            bannedListDtos.add(bannedListDto1);
        });
        try {
            ExcelUtils.exportExcel(bannedListDtos, "问答禁言表", "问答禁言", BannedListDto.class, "问答禁言", response);
        } catch (IOException e) {
            throw new ServiceException("导出异常:"+e.getMessage());
        }
    }

    @Override
    public ReturnVO deleteCommunityBanned(Banned banned){
        Long userId = banned.getUserId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("module_type",1);
        int delete = bannedMapper.delete(queryWrapper);
        if(delete==0){
            throw new ServiceException("该用户未被禁言");
        }
        //todo 小程序订阅
        SystemMessageMqDto systemMessageMqDto = new SystemMessageMqDto();
        systemMessageMqDto.setMessageDetail("您已被管理员解除封禁");
        systemMessageMqDto.setMessageTitle("社区");
        systemMessageMqDto.setWxUserIds(new ArrayList<Long>() {
            {
                add(userId);
            }
        });
        commentProducer.ackMQSender(JSONObject.toJSONString(systemMessageMqDto));
        return new ReturnVO("社区用户解封成功",1);
    }

    @Override
    public ReturnVO deleteQueAnsBanned(Banned banned){
        Long userId = banned.getUserId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("module_type",0);
        int delete = bannedMapper.delete(queryWrapper);
        if(delete==0){
            throw new ServiceException("该用户未被禁言");
        }
        //todo 小程序订阅
        SystemMessageMqDto systemMessageMqDto = new SystemMessageMqDto();
        systemMessageMqDto.setMessageDetail("您已被管理员解除封禁");
        systemMessageMqDto.setMessageTitle("问答");
        systemMessageMqDto.setWxUserIds(new ArrayList<Long>() {
            {
                add(userId);
            }
        });
        commentProducer.ackMQSender(JSONObject.toJSONString(systemMessageMqDto));
        return new ReturnVO("问答用户解封成功",1);
    }
}
